var TChannelAsThrift = require('tchannel/as/thrift');
var TChannel = require('tchannel');
var fs = require('fs');
var path = require('path');

var server = TChannel({serviceName: 'process_config_server'});

var client = TChannel();
var echoChannel = client.makeSubChannel({
    serviceName: 'process_config_servic1',
    peers: ['172.30.32.88:3011']
});
var tchannelAsThrift = TChannelAsThrift({
    channel: echoChannel,
    entryPoint: path.join(__dirname, 'thrift/config.prcessinfo.thrift')
});

// var context = {};
// tchannelAsThrift.register(server, 'Echo::echo', context, echo);
// function echo(context, req, head, body, callback) {
//     callback(null, {
//         ok: true,
//         head: head,
//         body: body
//     });
// }

server.listen(4042, '127.0.0.1', onListening);
function onListening() {
    tchannelAsThrift.request({
        serviceName: 'process_config_service',
        headers: {
            cn: 'echo'
        },
        hasNoParent: true
    }).send('ProcessServerService::GetServiceListHandlerImpl', {
        someHeader: 'headerValue'
    }, {
        value: 'some-string'
    }, onResponse);

    function onResponse(err, res) {
        if (err) {
            console.log('got error', err);
        } else {
            console.log('got response', res);
        }

        server.close();
        client.close();
    }
}